---
title: "Results from Student Pilot"
output: rmarkdown::null_document
---

---
title: "Main Results"
output: rmarkdown::null_document
---

Note: Run cleaning.Rmd first to clean raw survey data.

# Setup

```{r setup}

# Load Packages
library(pacman)

p_load(tidyverse, readxl, texreg, marginaleffects, ggpubr, stringr, scales, 
       Hmisc, broom, knitr, psych, xtable, kableExtra, ggrepel, ggforce, grid,
       ggsci)

# Student Sample -- Presented in SI
load("survey1_clean.rds")

# Theme for Plotting 
my_theme <-  theme(
    text = element_text(size=14, color = "black"),
    strip.text = element_text(face = "bold"),
    axis.text.y = element_text(color = "black"),
    axis.text.x = element_text(color = "black"),
    axis.title = element_text(color = "black"),
    panel.background = element_blank(), 
    panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(),  
    panel.border = element_rect(color = "black", fill = NA, linewidth = 1),  
    strip.background = element_rect(color = "black", fill = "white", linewidth = 1),
  )


```


# Run Models


```{r run-models}

# Vector of DVs
dvs <- c("trust", "stay_involved", "vote")

# Empty lists to store results
main_models <- list() # list of main effects
interactive_models <- list()  # list of interactive effects

# Loop through each DV and fit the models
for (i in 1:length(dvs)) {
  dv <- dvs[i]

  # Interactive effects
  inter_eff1 <- as.formula(paste(dv, "~ tx_scandal * tx_gender + hostile + benevolent + liberal"))
  inter_mod1 <- lm(inter_eff1, data = survey1)

  # Store results
  interactive_models[[dv]] <- inter_mod1
  
}

```

# Print Table for SI

```{r}


# Interactive Effects Study 1
texreg(interactive_models,
      file = "tables/inter_eff_student_sample.tex",
       dcolumn = T,
       custom.model.names = c("Trust", "Stay Involved", "Vote"),
       caption = "Interactive Effects (Student Sample).",
       caption.above = T,
       label = "si_inter_eff_student",
       custom.coef.map = list("(Intercept)" = "Intercept",
                              "tx_gendermale" = "Male Politician",
                              "tx_scandallie" = "Lie",
                              "tx_scandallie:tx_gendermale" = "$\\times$ Male Politician", 
                              "tx_scandalsexual-misconduct" = "Sex Scandal",
                              "tx_scandalsexual-misconduct:tx_gendermale" = "$\\times$ Male Politican",
                              "hostile" = "Hostile Sexism",
                              "benevolent" = "Benevolent Sexism", 
                              "liberal" = "Progressive Ideology"))



```



# Estimate Interactive Effects

```{r effect-of-gender}

# Dataframe to store results
all_comparisons <- data.frame()

# Iterate over the interactive models, estimating effect of gender by scandal
for (i in 1:length(dvs)) {
    
    # Get the model for the current DV and study
    model <- interactive_models[[dvs[i]]]
    
    # Effect of gender by scandal
    my_comparison <- avg_comparisons(model, variables = "tx_gender", by = "tx_scandal")
    
    
    # Add the name of the DV and study number
    my_comparison$dv <- dvs[i]
    
    # Bind them all together 
    all_comparisons <- rbind(all_comparisons, my_comparison)

  }



```


## Plot Gender Effects

```{r plot-effects}

# Recode my_comparisons for plotting 
all_comparisons <- all_comparisons |> 
  mutate(
    # Dependent variable
    DV = factor(case_match(dv,
                   "trust" ~ "Trust \nPolitician",
                   "stay_involved" ~ "Politician Should \nStay Involved",
                   "vote" ~ "Vote for \nPolitician"),
                levels = c("Trust \nPolitician", 
                           "Politician Should \nStay Involved",
                           "Vote for \nPolitician")),
    
    Scandal = factor(
      case_match(tx_scandal,
                 "corruption" ~ "Financial",
                 "lie" ~ "Dishonesty",
                 "sexual-misconduct" ~ "Sexual"),
      levels = rev(c("Sexual", "Financial", "Dishonesty"))), 
    
)

# Reverse factor levels for DV
all_comparisons$DV <- factor(all_comparisons$DV, 
                             levels = rev(levels(all_comparisons$DV)))

# Plot the Effect of Male, facet by study ------------------ #


# Lines to Seperate DVs
dv_lines <- all_comparisons |> 
  distinct(DV) |> 
  mutate(y_pos = as.numeric(factor(DV)))


# Plot
p <- ggplot(data = all_comparisons,  
       aes(y = DV, x = estimate, xmin = conf.low, xmax = conf.high, 
           color = Scandal, shape = Scandal)) +
  
  geom_hline(data = dv_lines |> filter(y_pos != min(y_pos)),
             aes(yintercept = y_pos - 0.5),
             linetype = "dashed", color = "grey70", linewidth = 0.4, inherit.aes = FALSE) +

  geom_pointrange(position = position_dodge(width = 0.5)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "red") +

  labs(y = "", x = "Male Politician - Female Politician",
       color = "Scandal \nCondition", shape = "Scandal \nCondition") +

  guides(colour = guide_legend(reverse = TRUE), 
         shape = guide_legend(reverse = TRUE)) +
  my_theme 

ggsave("figures/student_sample_effect_of_male.pdf", height = 4, width = 6.25)


```

